package com.yc.sakuralxm.uestcscanning.tools

import android.app.Activity
import android.content.Context
import android.provider.Settings
import android.view.View
import android.view.inputmethod.InputMethodInfo
import android.view.inputmethod.InputMethodManager

/**
 * Created by Administrator on 2016/9/7.
 */
object InputMethodUtil {
    private var imm: InputMethodManager? = null

    // 显示输入法
    fun show(context: Context, focusView: View) {
        imm = context
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm!!.showSoftInput(focusView, InputMethodManager.SHOW_FORCED)
    }

    // 隐藏输入发
    fun hide(activity: Activity) {
        val view = activity.window.peekDecorView()
        if (view != null && view.windowToken != null) {
            imm = activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm!!.hideSoftInputFromWindow(view.windowToken, 0)
            view.clearFocus()
        }
    }
    //            隐藏软键盘
    //            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    //调用该方法；键盘若显示则隐藏; 隐藏则显示
    fun toggle(context: Context) {
        imm = context
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm!!.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    //判断InputMethod的当前状态
    fun isShow(context: Context, focusView: View): Boolean {
        val obj = context.getSystemService(Context.INPUT_METHOD_SERVICE)
        println(obj)
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val bool = imm.isActive(focusView)
        val mInputMethodProperties = imm.enabledInputMethodList
        val N = mInputMethodProperties.size
        for (i in 0 until N) {
            val imi = mInputMethodProperties[i]
            if (imi.id == Settings.Secure.getString(context.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)) {
                //imi contains the information about the keyboard you are using
                return bool
            }
        }
        return false
    }
    //    public static boolean isShow(Activity activity, View focusView) {
    //        Log.i("---zzz---","softInputMode = "+activity.getWindow().getAttributes().softInputMode);
    //        return activity.getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;//_UNSPECIFIED;
    //    }
}